package org.ethan.user.controller;

import org.csource.common.MyException;
import org.ethan.basic.exception.BasicException;
import org.ethan.basic.util.AjaxResult;
import org.ethan.user.dto.LoginDto;
import org.ethan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService service;

    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = service.accountLogin(loginDto);
            // 向浏览器端返回登录信息，携带token
            return AjaxResult.me().setResultObj(map);
        } catch (BasicException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("System Failure!!!");
        }

    }

    /**
     * 微信三方登录
     * @param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult weChat(@RequestBody Map<String, String> map){
        try {
            return service.weChat(map);
        } catch (BasicException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("System Failure!!!");
        }

    }

    /**
     * 微信绑定
     * @param map
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> map){
        try {
            // service
            Map<String,Object> obj = service.binder(map);
            return AjaxResult.me().setResultObj(obj);
        }catch (BasicException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误");
        }
    }



}
